/*   1:    */ package org.jetbrains.io;
/*   2:    */ 
/*   3:    */

import com.intellij.ide.XmlRpcServer;
import com.intellij.ide.XmlRpcServer.SERVICE;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.util.net.NetUtils;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.QueryStringDecoder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.ide.CustomPortServerManager;
import org.jetbrains.ide.PooledThreadExecutor;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.util.Map;

/*   4:    */
/*   5:    */
/*   6:    */
/*   7:    */
/*   8:    */
/*   9:    */
/*  10:    */
/*  11:    */
/*  12:    */
/*  13:    */
/*  14:    */
/*  15:    */
/*  16:    */
/*  17:    */
/*  18:    */
/*  19:    */
/*  20:    */
/*  21:    */
/*  22:    */
/*  23:    */
/*  24:    */
/*  25:    */
/*  26:    */
/*  27:    */
/*  28:    */
/*  29:    */
/*  30:    */
/*  31:    */
/*  32:    */
/*  33:    */
/*  34:    */ 
/*  35:    */ public class BuiltInServer
/*  36:    */   implements Disposable
/*  37:    */ {
/*  38: 42 */   static final Logger LOG = Logger.getInstance(BuiltInServer.class);
/*  39:    */   private final ChannelRegistrar a;
/*  40:    */   
/*  41:    */   public BuiltInServer()
/*  42:    */   {
/*  43: 44 */     this.a = new ChannelRegistrar();
/*  44:    */   }
/*  45:    */   
/*  46:    */   public boolean isRunning()
/*  47:    */   {
/*  48: 47 */     return !this.a.isEmpty();
/*  49:    */   }
/*  50:    */   
/*  51:    */   public void start(int paramInt)
/*  52:    */   {
/*  53: 51 */     start(1, paramInt, 1, false);
/*  54:    */   }
/*  55:    */   
/*  56:    */   public int start(int paramInt1, int paramInt2, int paramInt3, boolean paramBoolean)
/*  57:    */   {
/*  58: 55 */     if (isRunning()) {
/*  59: 56 */       throw new IllegalStateException("server already started");
/*  60:    */     }
/*  61: 59 */     NioEventLoopGroup localNioEventLoopGroup = new NioEventLoopGroup(paramInt1, PooledThreadExecutor.INSTANCE);
/*  62: 60 */     ServerBootstrap localServerBootstrap = createServerBootstrap(localNioEventLoopGroup, this.a, null);
/*  63: 61 */     int i = a(paramInt2, paramInt3, paramBoolean, localServerBootstrap);
/*  64: 62 */     a(paramInt2, i, localNioEventLoopGroup);
/*  65: 63 */     return i;
/*  66:    */   }
/*  67:    */   
/*  68:    */   static ServerBootstrap createServerBootstrap(EventLoopGroup paramEventLoopGroup, ChannelRegistrar paramChannelRegistrar, @Nullable Map<String, Object> paramMap)
/*  69:    */   {
/*  70: 67 */     ServerBootstrap localServerBootstrap = NettyUtil.nioServerBootstrap(paramEventLoopGroup);
/*  71:    */     Object localObject;
/*  72: 68 */     if (paramMap == null)
/*  73:    */     {
/*  74: 69 */       localObject = new PortUnificationServerHandler();
/*  75: 70 */       localServerBootstrap.childHandler(new ChannelInitializer()
/*  76:    */       {
/*  77:    */         protected void initChannel(Channel paramAnonymousChannel)
/*  78:    */           throws Exception
/*  79:    */         {
/*  80: 73 */           paramAnonymousChannel.pipeline().addLast(new ChannelHandler[] { this.val$channelRegistrar, this.val$portUnificationServerHandler, ChannelExceptionHandler.getInstance() });
/*  81:    */         }
/*  82:    */       });
/*  83:    */     }
/*  84:    */     else
/*  85:    */     {
/*  86: 78 */       localObject = new XmlRpcDelegatingHttpRequestHandler(paramMap);
/*  87: 79 */       localServerBootstrap.childHandler(new ChannelInitializer()
/*  88:    */       {
/*  89:    */         protected void initChannel(Channel paramAnonymousChannel)
/*  90:    */           throws Exception
/*  91:    */         {
/*  92: 82 */           paramAnonymousChannel.pipeline().addLast(new ChannelHandler[] { this.val$channelRegistrar });
/*  93: 83 */           NettyUtil.addHttpServerCodec(paramAnonymousChannel.pipeline());
/*  94: 84 */           paramAnonymousChannel.pipeline().addLast(new ChannelHandler[] { this.val$handler, ChannelExceptionHandler.getInstance() });
/*  95:    */         }
/*  96:    */       });
/*  97:    */     }
/*  98: 88 */     return localServerBootstrap;
/*  99:    */   }
/* 100:    */   
/* 101:    */   private void a(int paramInt1, int paramInt2, NioEventLoopGroup paramNioEventLoopGroup)
/* 102:    */   {
/* 103: 92 */     for (CustomPortServerManager localCustomPortServerManager : (CustomPortServerManager[])CustomPortServerManager.EP_NAME.getExtensions()) {
/* 104:    */       try
/* 105:    */       {
/* 106: 94 */         int k = localCustomPortServerManager.getPort();
/* 107: 95 */         SubServer localSubServer = new SubServer(localCustomPortServerManager, paramNioEventLoopGroup);
/* 108: 96 */         Disposer.register(this, localSubServer);
/* 109: 97 */         if ((localCustomPortServerManager.isAvailableExternally()) || ((k != paramInt1) && (k != paramInt2))) {
/* 110: 98 */           localSubServer.bind(k);
/* 111:    */         }
/* 112:    */       }
/* 113:    */       catch (Throwable localThrowable)
/* 114:    */       {
/* 115:102 */         LOG.error(localThrowable);
/* 116:    */       }
/* 117:    */     }
/* 118:    */   }
/* 119:    */   
/* 120:    */   private int a(int paramInt1, int paramInt2, boolean paramBoolean, ServerBootstrap paramServerBootstrap)
/* 121:    */   {
/* 122:108 */     InetAddress localInetAddress = NetUtils.getLoopbackAddress();
/* 123:109 */     for (int i = 0; i < paramInt2; i++)
/* 124:    */     {
/* 125:110 */       int j = paramInt1 + i;
/* 126:113 */       if ((!SystemInfo.isLinux) && ((!SystemInfo.isWindows) || (SystemInfo.isWinVistaOrNewer))) {
/* 127:    */         try
/* 128:    */         {
/* 129:115 */           ServerSocket localServerSocket = new ServerSocket();
/* 130:    */           try
/* 131:    */           {
/* 132:117 */             localServerSocket.bind(new InetSocketAddress(j), 1);
/* 133:    */           }
/* 134:    */           finally
/* 135:    */           {
/* 136:120 */             localServerSocket.close();
/* 137:    */           }
/* 138:    */         }
/* 139:    */         catch (IOException localIOException)
/* 140:    */         {
/* 141:    */           continue;
/* 142:    */         }
/* 143:    */       }
/* 144:128 */       ChannelFuture localChannelFuture2 = paramServerBootstrap.bind(localInetAddress, j).awaitUninterruptibly();
/* 145:129 */       if (localChannelFuture2.isSuccess())
/* 146:    */       {
/* 147:130 */         this.a.add(localChannelFuture2.channel());
/* 148:131 */         return j;
/* 149:    */       }
/* 150:133 */       if ((!paramBoolean) && (i == paramInt2 - 1))
/* 151:    */       {
/* 152:134 */         LOG.error(localChannelFuture2.cause());
/* 153:135 */         return -1;
/* 154:    */       }
/* 155:    */     }
/* 156:139 */     LOG.info("We cannot bind to our default range, so, try to bind to any free port");
/* 157:140 */     ChannelFuture localChannelFuture1 = paramServerBootstrap.bind(localInetAddress, 0).awaitUninterruptibly();
/* 158:141 */     if (localChannelFuture1.isSuccess())
/* 159:    */     {
/* 160:142 */       this.a.add(localChannelFuture1.channel());
/* 161:143 */       return ((InetSocketAddress)localChannelFuture1.channel().localAddress()).getPort();
/* 162:    */     }
/* 163:146 */     LOG.error(localChannelFuture1.cause());
/* 164:147 */     return -1;
/* 165:    */   }
/* 166:    */   
/* 167:    */   public void dispose()
/* 168:    */   {
/* 169:153 */     this.a.close();
/* 170:154 */     LOG.info("web server stopped");
/* 171:    */   }
/* 172:    */   
/* 173:    */   public static void replaceDefaultHandler(@NotNull ChannelHandlerContext paramChannelHandlerContext, @NotNull SimpleChannelInboundHandler paramSimpleChannelInboundHandler)
/* 174:    */   {
/* 175:158 */     if (paramChannelHandlerContext == null) {
/* 176:158 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/jetbrains/io/BuiltInServer", "replaceDefaultHandler" }));
/* 177:    */     }
/* 178:158 */     if (paramSimpleChannelInboundHandler == null) {
/* 179:158 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "1", "org/jetbrains/io/BuiltInServer", "replaceDefaultHandler" }));
/* 180:    */     }
/* 181:158 */     paramChannelHandlerContext.pipeline().replace(DelegatingHttpRequestHandler.class, "replacedDefaultHandler", paramSimpleChannelInboundHandler);
/* 182:    */   }
/* 183:    */   
/* 184:    */   @ChannelHandler.Sharable
/* 185:    */   private static final class XmlRpcDelegatingHttpRequestHandler
/* 186:    */     extends DelegatingHttpRequestHandlerBase
/* 187:    */   {
/* 188:    */     private final Map<String, Object> a;
/* 189:    */     
/* 190:    */     public XmlRpcDelegatingHttpRequestHandler(Map<String, Object> paramMap)
/* 191:    */     {
/* 192:166 */       this.a = paramMap;
/* 193:    */     }
/* 194:    */     
/* 195:    */     protected boolean process(ChannelHandlerContext paramChannelHandlerContext, FullHttpRequest paramFullHttpRequest, QueryStringDecoder paramQueryStringDecoder)
/* 196:    */       throws IOException
/* 197:    */     {
/* 198:171 */       return ((paramFullHttpRequest.getMethod() == HttpMethod.POST) || (paramFullHttpRequest.getMethod() == HttpMethod.OPTIONS)) && (XmlRpcServer.SERVICE.getInstance().process(paramQueryStringDecoder.path(), paramFullHttpRequest, paramChannelHandlerContext, this.a));
/* 199:    */     }
/* 200:    */   }
/* 201:    */ }



/* Location:           E:\idea\

 * Qualified Name:     org.jetbrains.io.BuiltInServer

 * JD-Core Version:    0.7.0.1

 */